package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.TerminalUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.TerminalUploadResponse;
import io.renren.common.utils.DateUtil;
import io.renren.modules.basedata.dao.TerminalUploadDao;
import io.renren.modules.basedata.entity.tbl_terminal;
import io.renren.modules.basedata.service.TerminalUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;


@Service
public class TerminalUploadServiceImpl implements TerminalUploadService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private TerminalUploadDao terminalUploadDao;

    @Override
    public String terminalUpload() throws Exception {
        log.info("开始获取自助服务终端信息！");
        int operResult = 0;
        List<tbl_terminal> list = null;
        tbl_terminal terminal = null;
        list = getTerminalList();//获取自助服务终端信息
        if(list!=null&&list.size()>0){
            //一条一条上传数据
            for(int i=0;i<list.size();i++){
                terminal = list.get(i);
                TerminalUploadRequest request = new TerminalUploadRequest();
                request.setId(terminal.getId());
                request.setAddress(terminal.getAddress());
                request.setServiceItems(terminal.getServiceItems());
                request.setBusinessHours(terminal.getBusinessHours());
                request.setStartTime(DateUtil.formatDate(terminal.getStartTime()));
                request.setEndTime(DateUtil.formatDate(terminal.getEndTime()));
                request.setOperation(terminal.getFlag());
                String fileName = ETCCommHelper.getFileName(request,"65");
                //生成就json文件
//				CreateFileUtil.createJsonFile(request.toJsonStr(), "",fileName);
                try{
                    TerminalUploadResponse response = ETCCommHelper.sdtsUpload(fileName, request, TerminalUploadResponse.class);
                    log.info("上传自助服务终端信息成功,服务器返回:"+response.toJson());
                    if(response!=null&&response.getInfo().equals("成功！")){//表示上传成功
                        //更新自助服务终端信息表中的上传标志
                        if(terminalUploadDao.updateTerminalUploadflag(terminal.getId())==1){
                            log.info("更新自助服务终端信息表上传标志成功!");
                        }else{
                            log.info("更新自助服务终端信息表上传标志失败!");
                        }
                    }else{
                        log.info("上传自助服务终端信息失败!失败原因:"+response.getInfo());

                        terminalUploadDao.updateTerminalUploadflagError(terminal.getId());

                    }
                }catch(IOException e){
                    log.error("上传自助服务终端信息失败！错误信息:"+e.getMessage());


                    e.printStackTrace();
                    continue;
                }catch (ApiRequestException apie) {
                    log.error("上传自助服务终端信息失败！错误信息:"+apie.getMessage());

                    terminalUploadDao.updateTerminalUploadflagError(terminal.getId());
                    apie.printStackTrace();
                    continue;
                }
            }
            return "上传自助服务终端信息结束！";
        }else{
            return "无自助服务终端信息,上传结束...";
        }
    }

    //获取自助服务终端信息
    @SuppressWarnings("unchecked")
    public List<tbl_terminal> getTerminalList() throws IOException{
        List<tbl_terminal> list;
        try{
            list = terminalUploadDao.getTerminalList();
        }catch(Exception e){
            e.printStackTrace();
            log.error("获取自助服务终端信息失败！错误信息:",e);
            throw new IOException("获取自助服务终端信息失败:"+e.getMessage());
        }
        return list;
    }


}
